﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WoW_Beast
{
    public partial class SavedVariables : Form
    {
        String wtfPath = Program.wowDir + @"WTF\Account";
        public SavedVariables()
        {
            InitializeComponent();
        }
        
        private void SavedVariables_Load(object sender, EventArgs e)
        {
            //Check Saved Variables Directory
            if (!new DirectoryInfo(wtfPath).Exists)
            {
                MessageBox.Show("Could not find Account folder. Exiting.");
            }

            //Load Account list
            cboAccount.Items.Clear();
            foreach (DirectoryInfo dir in (new DirectoryInfo(wtfPath).GetDirectories()))
            {
                cboAccount.Items.Add(dir);
            }
            if (cboAccount.Items.Count != 0)
            {
                cboAccount.SelectedIndex = 0;
            }
            else
            {
                MessageBox.Show("No Accounts Found. Exiting.");
                Application.Exit();
            }
        }

        private void cboAccount_SelectedIndexChanged(object sender, EventArgs e)
        {
            cboChars.Items.Clear();
            cboChars.Items.Add("All Characters");
            foreach (DirectoryInfo dir in (new DirectoryInfo(wtfPath + @"\" + cboAccount.SelectedItem.ToString()).GetDirectories()))
            {
                if (dir.Name != "SavedVariables")
                {
                    foreach (DirectoryInfo dir2 in dir.GetDirectories())
                    {
                        cboChars.Items.Add(dir2 + " (" + dir + ")");
                    }
                }
            }
            cboChars.SelectedIndex = 0;
        }

        private void cboChars_SelectedIndexChanged(object sender, EventArgs e)
        {
            refreshVars();
        }

        private void refreshVars()
        {
            lstSavedVars.Items.Clear();

            DirectoryInfo top;
            if (cboChars.SelectedItem.ToString() == "All Characters")
            {
                top = new DirectoryInfo(wtfPath + @"\" + cboAccount.SelectedItem.ToString() + @"\SavedVariables");
            }
            else
            {
                //Get character info
                string realm, name;
                name = cboChars.SelectedItem.ToString();
                realm = name.Substring(name.IndexOf("(") + 1, (name.Length - name.IndexOf("(")) - 2);
                name = name.Replace(" (" + realm + ")", "");
                top = new DirectoryInfo(wtfPath + @"\" + cboAccount.SelectedItem.ToString() + @"\" + realm + @"\" + name + @"\SavedVariables");
            }


            try
            {
                foreach (FileInfo file in top.GetFiles("*.lua"))
                {
                    lstSavedVars.Items.Add(file.Name);
                }
            }
            catch
            {
                lstSavedVars.Items.Add("No SavedVariables found.");
            }

        }

        private void lblSelectNone_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            for (int i = 0; i <= lstSavedVars.Items.Count - 1; i++)
            {
                lstSavedVars.SetItemChecked(i, false);
            }
        }

        private void lblSelectAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            for (int i = 0; i <= lstSavedVars.Items.Count - 1; i++)
            {
                lstSavedVars.SetItemChecked(i, true);
            }
        }

        private void lblSelectUnused_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            for (int i = 0; i <= lstSavedVars.Items.Count - 1; i++)
            {
                if (new DirectoryInfo(Program.wowDir + @"Interface\Addons\" + lstSavedVars.Items[i].ToString().Replace(".lua", "")).Exists)
                {
                    lstSavedVars.SetItemChecked(i, false);
                }
                else
                {
                    lstSavedVars.SetItemChecked(i, true);
                }
            }
        }

        private void btnBackup_Click(object sender, EventArgs e)
        {
            foreach (object item in lstSavedVars.CheckedItems)
            {
                DirectoryInfo top;
                if (cboChars.SelectedItem.ToString() == "All Characters")
                {
                    top = new DirectoryInfo(wtfPath + @"\" + cboAccount.SelectedItem.ToString() + @"\SavedVariables");
                }
                else
                {
                    //Get character info
                    string realm, name;
                    name = cboChars.SelectedItem.ToString();
                    realm = name.Substring(name.IndexOf("(") + 1, (name.Length - name.IndexOf("(")) - 2);
                    name = name.Replace(" (" + realm + ")", "");
                    top = new DirectoryInfo(wtfPath + @"\" + cboAccount.SelectedItem.ToString() + @"\" + realm + @"\" + name + @"\SavedVariables");
                }

                if (File.Exists(top.FullName + @"\" + item.ToString()))
                {
                    DateTime now = DateTime.Now.Date;
                    File.Copy(top.FullName + @"\" + item.ToString(), top.FullName + @"\" + item.ToString().Replace(".lua", "." + now.Month + "-" + now.Day + "-" + now.Year + "-" + now.Hour + ".bak"), true);
                }
            }
            MessageBox.Show("Backup Complete");
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lstSavedVars.CheckedItems.Count < 1 || lstSavedVars.CheckedItems[0].ToString() == "No SavedVariables found.")
            {
                MessageBox.Show("No items selected");
                return;
            }

            String deleteMsg = "The following files will be deleted: \n";
            for (int i = 0; i <= lstSavedVars.CheckedItems.Count - 1; i++)
            {
                if (i < 16)
                {
                    String item = lstSavedVars.CheckedItems[i].ToString();
                    deleteMsg += item + "\n";
                }
                else if (i == 16)
                {
                    deleteMsg += "...";
                }
            }
            DialogResult cont = MessageBox.Show(deleteMsg, "File Delete Confirmation", MessageBoxButtons.YesNo);
            if (cont == DialogResult.No)
                return;

            foreach (object item in lstSavedVars.CheckedItems)
            {
                DirectoryInfo top;
                if (cboChars.SelectedItem.ToString() == "All Characters")
                {
                    top = new DirectoryInfo(wtfPath + @"\" + cboAccount.SelectedItem.ToString() + @"\SavedVariables");
                }
                else
                {
                    //Get character info
                    string realm, name;
                    name = cboChars.SelectedItem.ToString();
                    realm = name.Substring(name.IndexOf("(") + 1, (name.Length - name.IndexOf("(")) - 2);
                    name = name.Replace(" (" + realm + ")", "");
                    top = new DirectoryInfo(wtfPath + @"\" + cboAccount.SelectedItem.ToString() + @"\" + realm + @"\" + name + @"\SavedVariables");
                }

                if (File.Exists(top.FullName + @"\" + item.ToString()))
                {
                    File.Delete(top.FullName + @"\" + item.ToString());
                }
            }
            refreshVars();
        }
    }
}
